package com.bloveoacom.weichi.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.bloveoacom.base.BaseListActivity;
import com.bloveoacom.base.UserInfoBean;
import com.bloveoacom.util.Constant;
import com.bloveoacom.util.DialogUtil;
import com.bloveoacom.util.DisplayUtil;
import com.bloveoacom.util.KeyboardPatch;
import com.bloveoacom.util.StringUtils;
import com.bloveoacom.util.ToastUtil;
import com.bloveoacom.weichi.R;
import com.bloveoacom.weichi.adapter.MyComment2Adapter;
import com.bloveoacom.weichi.bean.CommentBean;
import com.bloveoacom.weichi.bean.RewardBean;
import com.bloveoacom.weichi.contract.AllCommentContract;
import com.bloveoacom.weichi.model.AllCommentModel;
import com.bloveoacom.weichi.presenter.AllCommentPresenter;
import com.bloveoacom.weight.InputDialog;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

import static com.bloveoacom.util.DataUtil.PL;
import static com.bloveoacom.util.DataUtil.getReward;
import static com.bloveoacom.util.DataUtil.showMsg;
import static com.bloveoacom.util.KeyboardUtil.focus;
import static com.bloveoacom.util.KeyboardUtil.showKeyboard;


/**
 * author: zhu.px
 * e-mail: 1271901673@qq.com
 * time  : 2018/7/12
 * desc  : 所有评论
 */
public class AllCommentActivity extends BaseListActivity<AllCommentPresenter, AllCommentModel,MyComment2Adapter> implements AllCommentContract.View {


    CommentBean commentBean;
    @BindView(R.id.tv_title)
    TextView tv_title;
    private int commentNum;

    private String commentId;

    @Override
    public int getLayoutResId() {
        return R.layout.activity_allcomments;
    }

    @Override
    public void initView() {
        super.initView();
        tv_title.setText(commentNum+"条回复");
        KeyboardPatch.assistActivity(findViewById(android.R.id.content));
    }


    @Override
    public void getBundleExtras(Bundle extras) {
        commentId = getIntent().getStringExtra(Constant.DATA0);
        commentNum = Integer.valueOf(getIntent().getStringExtra(Constant.DATA1));
    }

    @Override
    protected void initSetting() {
            mAdapter = new MyComment2Adapter(R.layout.rv_item_comment,new ArrayList<CommentBean.CommentSon>());

    }
    CommentBean.CommentSon bean;
    @Override
    public void initListener() {
        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                 bean = mAdapter.getData().get(position);
                  switch (view.getId()) {
                    case R.id.tv_content:
                        replayUserId = bean.replyUserId;
                        showKeyboard(etCount);
                        focus(etCount);
                        if(replayUserId.equals(UserInfoBean.getInstance().getUid())){
                            etCount.setHint(getString(R.string.comment_hint));
                        }else{
                            etCount.setHint(getString(R.string.reply,bean.replyUserName));
                        }
                        break;
                }
            }
        });
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                bean =  mAdapter.getData().get(position);
                replayUserId = bean.userId;
                showKeyboard(etCount);
                focus(etCount);
                etCount.setHint(getString(R.string.reply,bean.userName));
            }
        });

    }
    private void showInputPasswordDialog() {
        if (inputPasswordDialog == null) {
            inputPasswordDialog = new InputDialog(this, "输入密码", "Password", true, false, R.style.MyDialogStyle);
            inputPasswordDialog.setItemClickListener(new InputDialog.ItemClickListener() {
                @Override
                public void left() {

                }

                @Override
                public void right(String content) {
                    DialogUtil.dissDialog(inputPasswordDialog);
                    mPresenter.comment(commentId, replayUserId,"1",commentContent, content);
                }
            });
        }
        inputPasswordDialog.show();
    }

    private InputDialog inputPasswordDialog;
    private String commentContent="", replayUserId ="";
    @OnClick({R.id.tv_comment,R.id.iv_close})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_comment:
                commentContent = String.valueOf(etCount.getText()).trim();
                if (isLogin()) {
                    if (StringUtils.isEmpty(commentContent)) {
                        ToastUtil.makeShortText(mContext, R.string.please_input_comment_content);
                        return;
                    }
                    showInputPasswordDialog();
                }
                break;
            case R.id.iv_close:
                finish();
                break;
            default:
                break;
        }
    }
    @BindView(R.id.et_count)
    EditText etCount;


    @Override
    public void comment(RewardBean bean) {
        showMsg(mContext,bean,R.drawable.ic_like_reward,getString(R.string.has_comment),PL);
        etCount.getText().clear();
        tv_title.setText((commentNum++)+"条回复");
        etCount.setHint(getString(R.string.comment_hint));
        mPresenter.loadFirstData(false);
    }

    @Override
    public void commentList(CommentBean bean) {
        commentBean = bean;
        loadDataComplete(bean.commentSonList);
        mAdapter.setCommentBean(commentBean);

    }

    @Override
    public String getNewsId() {
        return null;
    }
}
